package com.wayhome.srpingbootmybatis.mbs.expertimport.service.impl;

import com.github.pagehelper.PageInfo;
import com.wayhome.srpingbootmybatis.config.ApiResult;
import com.wayhome.srpingbootmybatis.mbs.expertimport.bo.DipTldyManagerBO;
import com.wayhome.srpingbootmybatis.mbs.expertimport.dto.SetlTldyExportDTO;
import com.wayhome.srpingbootmybatis.mbs.expertimport.dto.SetlTldyExportDTO2;
import com.wayhome.srpingbootmybatis.mbs.expertimport.dto.SetlTldyInfoDTO;
import com.wayhome.srpingbootmybatis.mbs.expertimport.dto.SetlTldyInfoQueyDTO;
import com.wayhome.srpingbootmybatis.mbs.expertimport.service.DipTldyService;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Service
public class DipTldyServiceImpl implements DipTldyService {

    @Resource
    private DipTldyManagerBO dipTldyManagerBO;

    @Override
    public ApiResult<List<SetlTldyInfoDTO>> getDipTldyInfoList(SetlTldyInfoQueyDTO setlTldyInfoDTO) throws Exception {


        return dipTldyManagerBO.getDipTldyInfoList(setlTldyInfoDTO);
    }

    @Override
    public void exportDipTldyInfo(SetlTldyInfoQueyDTO setlTldyCodeCExample, HttpServletResponse httpResponse) throws Exception {
        dipTldyManagerBO.exportDipTldyInfo(setlTldyCodeCExample, httpResponse);
    }

    @Override
    public ApiResult<List<SetlTldyExportDTO>> queryExportTldyInfo(SetlTldyInfoQueyDTO queryDTO) throws Exception {
        return ApiResult.success(dipTldyManagerBO.queryExportTldyInfo(queryDTO));
    }

    @Override
    public ApiResult<List<SetlTldyExportDTO2>> queryExportTldyInfo2(SetlTldyInfoQueyDTO queryDTO) throws Exception {
        queryDTO.setPageNum(1);
        queryDTO.setPageSize(10);
        boolean hasNextPage = true;
        List<SetlTldyExportDTO2> list = new ArrayList<>();
        do{
            PageInfo<SetlTldyExportDTO2> pageInfo = dipTldyManagerBO.queryExportTldyInfoNew(queryDTO);
            List<SetlTldyExportDTO2> data = pageInfo.getList();
            list.addAll(data);
            if (pageInfo.isIsLastPage() || CollectionUtils.isEmpty(data)) {
                hasNextPage = false;
                continue;
            }
            queryDTO.setPageNum(queryDTO.getPageNum() + 1);

        }while(hasNextPage);
        return ApiResult.success(list);
    }
}
